unit KnjigaPrejetihRacunovRpt;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DBBaseRpt, DokumentRpt, DB, DBClient, alBaseDataSet, MConnect,
  EXLReportExcelTLB, EXLReportBand, EXLReport;

type
  TrptKnjigaPrejetihRacunov = class(TrptDokument)
    procedure OnFormatCell(Sender: TObject; Band: TEXLReportBand; RowInTemplate,
      RowInReport, ColumnInReport: Integer; Report: _Worksheet;
      const CellValue: OleVariant);
  private
    { Private declarations }
  protected
    function GetSessionParamRelations(DataSet: TDataSet): TSessionParamRelations; override;
    procedure Initialize; override;
    procedure OnCalcExpression(Sender: TEXLReportBand; var GroupValue: String);
  public
    { Public declarations }
  end;

var
  rptKnjigaPrejetihRacunov: TrptKnjigaPrejetihRacunov;

implementation

{$R *.dfm}

{ TrptKnjigaPrejetihRacunov }

function TrptKnjigaPrejetihRacunov.GetSessionParamRelations(
  DataSet: TDataSet): TSessionParamRelations;
begin
  if DataSet = cdsMain then
  begin
    SetLength(Result, 1);
    Result[0].FieldName := 'REF_ID';
    Result[0].SessionName := 'POSLOVNO_LETO_ID';
  end;
end;

procedure TrptKnjigaPrejetihRacunov.Initialize;
var
  TitleBand: TExlReportBand;
  GroupHeaderBand: TExlReportGroupHeaderBand;
  MasterDataBand: TExlReportBand;
  GroupFooterBand: TExlReportGroupFooterBand;
begin
  inherited;
  TitleBand := rptMain.Bands.AddBand(xlrbtTitle);
  TitleBand.Range := 'A1:N7';
  GroupHeaderBand := TExlReportGroupHeaderBand(rptMain.Bands.AddBand(xlrbtGroupHeader));
  GroupHeaderBand.Range := 'A8:N9';
  GroupHeaderBand.OnCalcExpression := OnCalcExpression;
  MasterDataBand := rptMain.Bands.AddBand(xlrbtMasterData);
  MasterDataBand.Range := 'A10:N11';
  GroupFooterBand := TExlReportGroupFooterBand(rptMain.Bands.AddBand(xlrbtGroupFooter));
  GroupFooterBand.Range := 'A12:N13';
  GroupHeaderBand.Footer := GroupFooterBand;
end;

procedure TrptKnjigaPrejetihRacunov.OnCalcExpression(Sender: TEXLReportBand;
  var GroupValue: String);
begin
  GroupValue := FormatDateTime('yyyymm', cdsMain.FieldByName('DATUM').AsDateTime);
end;

procedure TrptKnjigaPrejetihRacunov.OnFormatCell(Sender: TObject;
  Band: TEXLReportBand; RowInTemplate, RowInReport, ColumnInReport: Integer;
  Report: _Worksheet; const CellValue: OleVariant);
var
  R1, R2: string;
begin
  inherited;
  if (Band.BandType = xlrbtGroupHeader) and (ColumnInReport = 1) then
  begin
    R1 := 'A' + IntToStr(RowInReport);
    R2 := 'N' + IntToStr(RowInReport);
    if (RowInTemplate = 8) and (RowInReport > RowInTemplate + 1) then
      Report.Range[R1, R2].EntireRow.PageBreak := xlPageBreakManual;
  end;
end;

initialization
  RegisterClass(TrptKnjigaPrejetihRacunov);

end.
